home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / gimpui.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-25  |  5.0 KB  |  197 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpui.c
  5.  * Copyright (C) 1999 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22. #include "config.h"
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. #include <gtk/gtk.h>
  28.  
  29. #include "gimpui.h"
  30.  
  31. #include "libgimp/gimpintl.h"
  32.  
  33.  
  34. extern gchar *prog_name;
  35.  
  36. static void  gimp_message_box_close_callback  (GtkWidget *widget,
  37.                            gpointer   data);
  38.  
  39. /*
  40.  *  Message Boxes...
  41.  */
  42.  
  43. typedef struct _MessageBox MessageBox;
  44.  
  45. struct _MessageBox
  46. {
  47.   GtkWidget   *mbox;
  48.   GtkWidget   *repeat_label;
  49.   gchar       *message;
  50.   gint         repeat_count;
  51.   GtkCallback  callback;
  52.   gpointer     data;
  53. };
  54.  
  55. /*  the maximum number of concucrrent dialog boxes */
  56. #define MESSAGE_BOX_MAXIMUM  4 
  57.  
  58. static GList *message_boxes = NULL;
  59.  
  60. void
  61. gimp_message_box (gchar       *message,
  62.           GtkCallback  callback,
  63.           gpointer     data)
  64. {
  65.   MessageBox *msg_box;
  66.   GtkWidget  *mbox;
  67.   GtkWidget  *vbox;
  68.   GtkWidget  *label;
  69.   GList      *list;
  70.  
  71.   if (!message)
  72.     return;
  73.  
  74.   if (g_list_length (message_boxes) > MESSAGE_BOX_MAXIMUM)
  75.     {
  76.       fprintf (stderr, "%s: %s\n", prog_name, message);
  77.       return;
  78.     }
  79.  
  80.   for (list = message_boxes; list; list = list->next)
  81.     {
  82.       msg_box = list->data;
  83.       if (strcmp (msg_box->message, message) == 0)
  84.     {
  85.       msg_box->repeat_count++;
  86.       if (msg_box->repeat_count > 1)
  87.         {
  88.           gchar *text = g_strdup_printf (_("Message repeated %d times"), 
  89.                          msg_box->repeat_count);
  90.           gtk_label_set_text (GTK_LABEL (msg_box->repeat_label), text);
  91.           g_free (text);
  92.           gdk_window_raise (msg_box->mbox->window);
  93.         }
  94.       else
  95.         {
  96.           GtkWidget *hbox;
  97.  
  98.           hbox = gtk_hbox_new (FALSE, 0);
  99.           gtk_box_pack_start (GTK_BOX (GTK_DIALOG (msg_box->mbox)->action_area), 
  100.                   hbox, TRUE, FALSE, 4);
  101.           msg_box->repeat_label = gtk_label_new (_("Message repeated once"));
  102.           gtk_container_add (GTK_CONTAINER (hbox), msg_box->repeat_label);
  103.  
  104.           gtk_widget_show (msg_box->repeat_label);
  105.           gtk_widget_show (hbox);
  106.           gdk_window_raise (msg_box->mbox->window);
  107.         }
  108.       return;
  109.     }
  110.     }
  111.  
  112.   if (g_list_length (message_boxes) == MESSAGE_BOX_MAXIMUM)
  113.     {
  114.       fprintf (stderr, "%s: %s\n", prog_name, message);
  115.       message = _("WARNING:\n"
  116.           "Too many open message dialogs.\n"
  117.           "Messages are redirected to stderr.");
  118.     }
  119.   
  120.   msg_box = g_new0 (MessageBox, 1);
  121.  
  122.   mbox = gimp_dialog_new (_("GIMP Message"), "gimp_message",
  123.               NULL, NULL,
  124.               GTK_WIN_POS_MOUSE,
  125.               FALSE, FALSE, FALSE,
  126.  
  127.               _("OK"), gimp_message_box_close_callback,
  128.               msg_box, NULL, NULL, TRUE, TRUE,
  129.  
  130.               NULL);
  131.  
  132.   vbox = gtk_vbox_new (FALSE, 0);
  133.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  134.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (mbox)->vbox), vbox);
  135.   gtk_widget_show (vbox);
  136.  
  137.   label = gtk_label_new (message);
  138.   gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  139.   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
  140.   gtk_widget_show (label);
  141.  
  142.   msg_box->mbox = mbox;
  143.   msg_box->message = g_strdup (message);
  144.   msg_box->callback = callback;
  145.   msg_box->data = data;
  146.  
  147.   message_boxes = g_list_append (message_boxes, msg_box);
  148.  
  149.   gtk_widget_show (mbox);
  150. }
  151.  
  152. static void
  153. gimp_message_box_close_callback (GtkWidget *widget,
  154.                  gpointer   data)
  155. {
  156.   MessageBox *msg_box;
  157.  
  158.   msg_box = (MessageBox *) data;
  159.  
  160.   /*  If there is a valid callback, invoke it  */
  161.   if (msg_box->callback)
  162.     (* msg_box->callback) (widget, msg_box->data);
  163.  
  164.   /*  Destroy the box  */
  165.   gtk_widget_destroy (msg_box->mbox);
  166.   
  167.   /* make this box available again */
  168.   message_boxes = g_list_remove (message_boxes, msg_box);
  169.  
  170.   g_free (msg_box->message);
  171.   g_free (msg_box);
  172. }
  173.  
  174.  
  175. /*  
  176.  * A workaround for what I think is a GTK+ bug:
  177.  *  If a dialog is hidden using gtk_widget_hide(),
  178.  *  and was iconified before, it is still present 
  179.  *  in the window_list and can be deiconified by
  180.  *  the user later. All subsequent calls to 
  181.  *  gtk_widget_hide() will then fail since the state
  182.  *  of the widget is still INVISIBLE.
  183.  *  Calling gdk_window_withdraw() seems to solve this.
  184.  *                                         --Sven
  185.  */
  186. void
  187. gimp_dialog_hide (GtkWidget *dialog)
  188. {
  189.   g_return_if_fail (dialog != NULL && !GTK_WIDGET_NO_WINDOW (dialog));
  190.   
  191.   gtk_widget_hide (dialog);
  192.   gdk_window_withdraw (dialog->window);
  193. }
  194.  
  195.  
  196.     
  197.